﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using QLChoDiaOc.Process;
using System.Data;

namespace QLChoDiaOc.Forms
{
    /// <summary>
    /// Interaction logic for FormDiaOcChuaBan.xaml
    /// </summary>
    public partial class FormDanhSachDiaOc : Page
    {
        ShowInfo _showInfo = new ShowInfo();
        UpdateInfo _updateInfo = new UpdateInfo();
        SelectInfo _selectInfo = new SelectInfo();
        OtherHandle _otherHandle = new OtherHandle();
        static bool m_bSelectData = false;
        static DataTable _dataTable = new DataTable();

        public FormDanhSachDiaOc()
        {
            InitializeComponent();
            _showInfo.ShowListDiaOc(dataDiaOc);
            AddComboBox();
            btnReport.Visibility = Visibility.Collapsed;
        }

        void AddComboBox()
        {
            loaiDiaOcComboBox.Items.Clear();
            tinhTrangComboBox.Items.Clear();
            huongNhaComboBox.Items.Clear();

            for (int i = 0; i < _selectInfo.SelectAllTenLoaiDiaOc().Count; i++)
                loaiDiaOcComboBox.Items.Add(_selectInfo.SelectAllTenLoaiDiaOc()[i]);

            tinhTrangComboBox.Items.Add("Đã Bán");
            tinhTrangComboBox.Items.Add("Chưa Bán");

            huongNhaComboBox.Items.Add("Đông");
            huongNhaComboBox.Items.Add("Tây");
            huongNhaComboBox.Items.Add("Nam");
            huongNhaComboBox.Items.Add("Bắc");
            huongNhaComboBox.Items.Add("Đông Nam");
            huongNhaComboBox.Items.Add("Đông Bắc");
            huongNhaComboBox.Items.Add("Tây Nam");
            huongNhaComboBox.Items.Add("Tây Bắc");
            huongNhaComboBox.Items.Add("");
        }

        bool KiemTraNhap()
        {
            if (maDiaOcTextBox.Text == "" ||
                maKHTextBox.Text == "" ||
                soNhaTextBox.Text == "" ||
                duongTextBox.Text == "" ||
                quanTextBox.Text == "" ||
                dienTichDatTextBox.Text == "" ||
                dienTichSuDungTextBox.Text == "" ||
                viTriTextBox.Text == "" ||
                moTaTextBox.Text == "" ||
                giaBantextBox.Text == "" ||
                huongNhaComboBox.SelectedItem == null ||
                tinhTrangComboBox.SelectedItem == null ||
                loaiDiaOcComboBox.SelectedItem == null)
                return true;
            else 
                return false;
        }

        string TenTinhTrangDiaOc(int TinhTrang)
        {
            string temp = "";
            switch (TinhTrang)
            {
                case 0:
                    temp = "Chưa Bán";
                    break;
                case 1:
                    temp = "Đã Bán";
                    break;
                default: 
                    break;
            }
            return temp;
        }



        int MaTinhTrangDiaOc(string TenTinhTrang)
        {
            int temp = -1;
            switch (TenTinhTrang)
            {
                case "Chưa Bán":
                    temp = 0;
                    break;
                case "Đã Bán":
                    temp = 1;
                    break;
                default:
                    break;
            }
            return temp;
        }

        void ClearIndex()
        {
            maDiaOcTextBox.Text = "";
            maKHTextBox.Text = "";
            loaiDiaOcComboBox.SelectedItem = null;
            viTriTextBox.Text = "";
            soNhaTextBox.Text = "";
            duongTextBox.Text = "";
            quanTextBox.Text = "";
            huongNhaComboBox.SelectedItem =null;
            huongNhaComboBox.Text = "";
            dienTichDatTextBox.Text = "";
            dienTichSuDungTextBox.Text = "";
            giaBantextBox.Text = "";
            moTaTextBox.Text = "";
            tinhTrangComboBox.SelectedItem = null;
        }

        private void dataDiaOc_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if(!m_bSelectData)
            {
                var item = dataDiaOc.SelectedItem as DIAOC;

                btnDaBan.Visibility = Visibility.Collapsed;
                btnSuaDiaOc.IsEnabled = true;

                maDiaOcTextBox.Text = item.MaDiaOc;
                maKHTextBox.Text = item.MaKH;
                loaiDiaOcComboBox.SelectedItem = _selectInfo.SelectTenLoaiDiaOc(item.MaLoaiDiaOc);
                viTriTextBox.Text = item.ViTri;
                soNhaTextBox.Text = item.SoNha;
                duongTextBox.Text = item.Duong;
                quanTextBox.Text = item.Quan;
                huongNhaComboBox.Text = item.HuongNha.ToString();
                dienTichDatTextBox.Text = item.DienTichDat.ToString();
                dienTichSuDungTextBox.Text = item.DienTichSuDung.ToString();
                giaBantextBox.Text = item.GiaBan;
                moTaTextBox.Text = item.MoTa;
                tinhTrangComboBox.SelectedItem = TenTinhTrangDiaOc(item.TinhTrang);

                if (item.TinhTrang == 0)
                    btnDaBan.Visibility = Visibility.Visible;
                if(item.TinhTrang == 1)
                    btnSuaDiaOc.IsEnabled = false;
            }
            else
            {
                m_bSelectData = false;
                _showInfo = new ShowInfo();
                _showInfo.ShowListDiaOc(dataDiaOc);
            }
        }

        private void btnTimKiem_Click(object sender, RoutedEventArgs e)
        {
            if (!m_bSelectData)
                m_bSelectData = true;
            ClearIndex();
            m_bSelectData = false;
            maDiaOcTextBox.IsEnabled = true;
            btnTimDiaOc.IsEnabled = true;
            maKHTextBox.IsEnabled = true;
            btnTimKiemTheoKhachHang.IsEnabled = true;
        }

        private void btnSuaDiaOc_Click(object sender, RoutedEventArgs e)
        {
            if (KiemTraNhap())
                MessageBox.Show("Bạn chưa nhập đầy đủ thông tin", "Thông Báo");
            else
            {
                _updateInfo.UpdateDiaOc(maDiaOcTextBox.Text,
                                        maKHTextBox.Text,
                                        _selectInfo.SelectMaLoaiDiaOc(loaiDiaOcComboBox.SelectedItem.ToString()),
                                        soNhaTextBox.Text,
                                        duongTextBox.Text,
                                        quanTextBox.Text,
                                        float.Parse(dienTichDatTextBox.Text),
                                        float.Parse(dienTichSuDungTextBox.Text),
                                        viTriTextBox.Text,
                                        huongNhaComboBox.Text,
                                        moTaTextBox.Text,
                                        giaBantextBox.Text,
                                        MaTinhTrangDiaOc(tinhTrangComboBox.SelectedItem.ToString()));
                if (!m_bSelectData)
                    m_bSelectData = true;
                ClearIndex();
                dataDiaOc.ItemsSource = null;
            }
        }

        private void bntReloadDiaOc_Click(object sender, RoutedEventArgs e)
        {
            if (!m_bSelectData)
                m_bSelectData = true;
            ClearIndex();
            m_bSelectData = false;
            _showInfo = new ShowInfo();
            _showInfo.ShowListDiaOc(dataDiaOc);
        }

        private void btnDaBan_Click(object sender, RoutedEventArgs e)
        {
            _updateInfo.UpdateDiaOc(maDiaOcTextBox.Text, 1);
            if (!m_bSelectData)
                m_bSelectData = true;
            ClearIndex();
            dataDiaOc.ItemsSource = null;
        }

        private void btnTimDiaOc_Click(object sender, RoutedEventArgs e)
        {
            _showInfo = new ShowInfo();
            _showInfo.ShowListDiaOc(dataDiaOc, maDiaOcTextBox.Text);
            maKHTextBox.IsEnabled = false;
            btnTimKiemTheoKhachHang.IsEnabled = false;
            maDiaOcTextBox.IsEnabled = false;
            btnTimDiaOc.IsEnabled = false;
        }

        private void btnTimKiemTheoKhachHang_Click(object sender, RoutedEventArgs e)
        {
            _showInfo = new ShowInfo();
            _showInfo.ShowListDiaOc_MaKH(dataDiaOc, maKHTextBox.Text, ref _dataTable);
            maKHTextBox.IsEnabled = false;
            btnTimKiemTheoKhachHang.IsEnabled = false;
            maDiaOcTextBox.IsEnabled = false;
            btnTimDiaOc.IsEnabled = false;
            btnReport.Visibility = Visibility.Visible;
        }

        private void btnReport_Click(object sender, RoutedEventArgs e)
        {
            DataSet _dataSet = new DataSet();
            _dataSet = _otherHandle.ConvertDataTableToDataSet(_dataTable);
            Export.exportExcel("Danh Sách Địa Ốc Theo Khách Hàng", _dataSet);
        }
    }
}
